﻿using System;
using System.IO;
using System.Web;
using System.Xml;
using SyCODE.Component;

namespace SyCODE.Blog.Uploader
{
    /// <summary>
    /// FileList.aspx 的处理程序
    /// </summary>
    public class FileListHandler : IHttpHandler
    {
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Expires = 0;
            context.Response.ContentType = "text/xml";

            string path = Path.Combine(AppPath.GetUserPhysicalPath(context.Request.Form["u"]), "upfiles");

            XmlTextWriter writer = new XmlTextWriter(context.Response.OutputStream, context.Response.ContentEncoding);
            writer.WriteStartDocument();
            writer.WriteStartElement("root");

            if (Directory.Exists(path))
            {
                DirectoryInfo info = new DirectoryInfo(path);
                FileInfo[] fis = info.GetFiles();
                string dateformat = StringResource.GetField("dateformat");
                for (int i = 0; i < fis.Length; i++)
                {
                    writer.WriteStartElement("f");

                    writer.WriteAttributeString("fn", fis[i].Name);
                    writer.WriteAttributeString("s", fis[i].Length.ToString());
                    writer.WriteAttributeString("d", fis[i].LastAccessTime.ToString(dateformat));

                    writer.WriteEndElement();
                }
            }
            else
            {
                Directory.CreateDirectory(path);
            }
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
        }
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}